package com.example.demo.example.properties;

import java.io.File;
import java.util.*;

/**
 * @author libo
 * @ClassName MainCompare
 * @Description: 指定配置文件，将配置文件根据value,key排序，之后同向输出进行比对
 * @date 2019/3/4 10:42
 */
public class MainCompare {
    public static void main(String[] args) {
        List<String> files = getFileList();
        for(int i=0;i<files.size();i++) {
            System.out.print("\""+files.get(i)+"\"");
            for (Map.Entry<String, String> e : getKeyValues(files.get(0)).entrySet()) {
                String key = e.getKey();
                String value = e.getValue();
                System.out.print("\"" + key + "\",");
            }
            System.out.println("");
            for(Map.Entry<String, String> e : getKeyValues(files.get(i)).entrySet()){
                String key = e.getKey();
                if(!isHave(files.get(i), key)){
                    System.out.print(",null");
                }else{
                    String newValue = getValue(files.get(i), key);
                    if(!newValue.trim().equals("")){
                        System.out.print(",\""+newValue+"\"");
                    }else{
                        System.out.print(",\"\"");
                    }
                }
            }
            System.out.println("");
        }
    }
    public static  List<String> getFileList(){
        List<String> result = new ArrayList<>();
        result.add("D:\\Ideaworkspace\\map-usercenter\\map-usercenter-service\\src\\main\\resources\\properties\\dev.properties");
        result.add("D:\\Ideaworkspace\\map-usercenter\\map-usercenter-service\\src\\main\\resources\\properties\\online.properties");
        return  result;
    }

    public static LinkedHashMap<String,String> getKeyValues(String file){
        LinkedHashMap<String,String> result = new LinkedHashMap<>();
        Properties properties = PropertiesUtils.getProperties(file);
        List<String[]> temp = new ArrayList<>();
        for(Map.Entry<Object, Object> e :properties.entrySet()){
            String key = (String) e.getKey();
            String value = (String)e.getValue();
            temp.add(new String[]{key,value});
        }
        Collections.sort(temp, new Comparator<String[]>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                int sort1 = o1[1].compareTo(o2[1]);
                int sort2 = o1[0].compareTo(o2[0]);
                if(sort1 !=0 ){
                    return  sort1;
                }
                return  sort2;
            }
        });
        for (String []  s : temp) {
            result.put(s[0],s[1]);
        }
        return  result;
    }

    public static String getValue(String file,String key){
        return  PropertiesUtils.getString(file,key);
    }

    public static boolean isHave(String file,String key){
        return PropertiesUtils.map.get(file).containsKey(key);
    }
}
